const util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    res:[],
    ista:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ista = options.ista;
    this.setData({
      ista: ista
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var data = { user_id:app.globalData.userInfo.id};
    util.commonRequest("index/Address/index",'post', data, function (res) {
      if (res.data.code == 200) {
        that.setData({
          res:res.data.data
        })
      } else {
        wx.showToast({
          icon: "none",
          title: '服务器处理异常',
        })
      }
    });
  },
  selradio:function(e){
    var id = e.currentTarget.dataset.id;
    var that = this;
    // var user = wx.getStorageSync("userinfo");
    var data = { id: id, userid: app.globalData.userInfo.id};
    util.commonRequest("index/Address/setDetault", 'post',data, function (res) {
        that.onShow();
        if (that.data.ista == 1){
          wx.navigateBack({
            delta: 1
          })
        }
    });
  },
  deleteAddress:function(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '你确定要删除该地址吗？删除后不可恢复!',
      success: function (res) {
        if (!res.cancel) {
          var id = e.currentTarget.dataset.id;
          // var that = this;
          // var user = wx.getStorageSync("userinfo");
          var data = { addressid: id, userid: app.globalData.userInfo.id };
          util.commonRequest("index/Address/delete",'post', data, function (res) {
            that.onShow();
          });
        }
      }
    });
  },
  //添加新地址
  add:function(){
    wx.navigateTo({
      url: '../add_address/index',
    })
  },
 //编辑地址
  editAddress:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../edit_address/index?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})